﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Dao;

namespace Sweepids.Models.Object
{
    public class User
    {
        //Private members
        private bool _fill = false;
        private CreditCard _creditCardObj;
        private Zip _zipObj;
        private List<Item> _bidOnItemList;
        private List<Item> _loseItemList;
        private List<Item> _wonItemList; 
        //Columns
        public string Username { get; set; }
        public string CreditCard { get; set; }
        public string Email { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string Zip { get; set; }
        public decimal SweepsOwned { get; set; }
        public bool IsAdmin { get; set; }
        public string Password { get; set; }
        //Foreign Keys
        public CreditCard CreditCardObj { 
            get
            {
                if (!_fill)
                {
                    var creditCardDao = new CreditCardDao();
                    _creditCardObj = creditCardDao.Read(CreditCard);
                }
                return _creditCardObj;
            } 
            set
            {
                _creditCardObj = value;
                CreditCard = value.CardNum;
            }
        }
        public Zip ZipObj
        {
            get
            {
                if (!_fill)
                {
                    var zipDao = new ZipDao();
                    _zipObj = zipDao.Read(Zip);
                }
                return _zipObj;
            }
            set
            {
                _zipObj = value;
                Zip = value.ZipId;
            }
        }
        //Extra details
        public List<Item> BidOnItemList
        {
            get
            {
                if (!_fill)
                {
                    var bidOnDao = new BidOnDao();
                    var bidOnList = bidOnDao.Read(Username);
                    _bidOnItemList = bidOnList.Select(bidOn => bidOn.ItemObj).ToList();
                }
                return _bidOnItemList;
            }
        }
        public List<Item> LoseItemList
        {
            get
            {
                if (!_fill)
                {
                    var loseDao = new LoseDao();
                    var loseList = loseDao.Read(Username);
                    _loseItemList = loseList.Select(lose => lose.ItemObj).ToList();
                }
                return _loseItemList;
            }
        }
        public List<Item> WonItemsList
        {
            get
            {
                if (!_fill)
                {
                    var itemDao = new ItemDao();
                    _wonItemList = itemDao.ReadUserWins(Username);
                }
                return _wonItemList;
            }
        } 

        //Oracle parameters
        public List<OracleParameter> OracleParameters
        {
            get
            {
                return new List<OracleParameter>
                    {
                        new OracleParameter(":USERNAME", OracleDbType.Varchar2)
                            {
                                Value = Username ?? ""
                            },
                        new OracleParameter(":CREDIT_CARD", OracleDbType.Varchar2)
                            {
                                Value = CreditCard ?? ""
                            },
                        new OracleParameter(":EMAIL", OracleDbType.Varchar2)
                            {
                                Value = Email ?? ""
                            },
                        new OracleParameter(":FIRST_NAME", OracleDbType.Varchar2)
                            {
                                Value = FirstName ?? ""
                            },
                        new OracleParameter(":LAST_NAME", OracleDbType.Varchar2)
                            {
                                Value = LastName ?? ""
                            },
                        new OracleParameter(":ADDRESS", OracleDbType.Varchar2)
                            {
                                Value = Address ?? ""
                            },
                        new OracleParameter(":ZIP", OracleDbType.Varchar2)
                            {
                                Value = Zip ?? ""
                            },
                        new OracleParameter(":SWEEPS_OWNED", OracleDbType.Decimal)
                            {
                                Value = SweepsOwned
                            },
                        new OracleParameter(":IS_ADMIN", OracleDbType.Decimal)
                            {
                                Value = IsAdmin
                            },
                        new OracleParameter(":PASSWORD", OracleDbType.Varchar2)
                            {
                                Value = Password ?? ""
                            }
                    };
            }
        }
         //Constructors
        public User()
        {
            Username = "";
            CreditCard = "";
            Email = "";
            FirstName = "";
            LastName = "";
            Address = "";
            Zip = "";
            Password = "";
        }

        public User(IDataRecord myReader)
        {
            Username = myReader["USERNAME"] == DBNull.Value ? "" : (string) myReader["USERNAME"];
            CreditCard = myReader["CREDIT_CARD"] == DBNull.Value ? "" : (string) myReader["CREDIT_CARD"];
            Email = myReader["EMAIL"] == DBNull.Value ? "" : (string) myReader["EMAIL"];
            FirstName = myReader["FIRST_NAME"] == DBNull.Value ? "" : (string) myReader["FIRST_NAME"];
            LastName = myReader["LAST_NAME"] == DBNull.Value ? "" : (string) myReader["LAST_NAME"];
            Address = myReader["ADDRESS"] == DBNull.Value ? "" : (string) myReader["ADDRESS"];
            Zip = myReader["ZIP"] == DBNull.Value ? "" : (string) myReader["ZIP"];
            SweepsOwned = myReader["SWEEPS_OWNED"] == DBNull.Value ? 0 : (decimal) myReader["SWEEPS_OWNED"];
            IsAdmin = myReader["IS_ADMIN"] != DBNull.Value && (((decimal) myReader["IS_ADMIN"]) != 0);
            Password = myReader["PASSWORD"] == DBNull.Value ? "" : (string) myReader["PASSWORD"];
        }
        //Methods
        public void Fill()
        {
            _fill = false;
            _creditCardObj = CreditCardObj;
            _zipObj = ZipObj;
            _bidOnItemList = BidOnItemList;
            _loseItemList = LoseItemList;
            _wonItemList = WonItemsList;
            _fill = true;
        }
    }
}